package letsjava.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class SocketThread implements Runnable {
	private Socket incoming;
	public SocketThread(Socket incoming){
		this.incoming = incoming;
	}
	
	public void run(){
		InputStream inStream;
        try {
	        inStream = incoming.getInputStream();
	        OutputStream outStream = incoming.getOutputStream();
			
			Scanner in = new Scanner(inStream);
			
			//PrintWriter out = new PrintWriter(outStream,true);
			PrintStream out = new PrintStream(outStream,true/*auto flush*/);
			
			out.println("Welcome to Socket Server, type BYE to exit");
			
			boolean done = false;
			while(!done && in.hasNextLine()){
				String line = in.nextLine();
				out.println("Echo :" + line);
				if(line.trim().equals("BYE")){
					done = true;
				}
			}
			
			out.println("BYE! ");
        } catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }	
		
	}
}
